package com.codefish.framework.spring.core.io.support;

import com.codefish.framework.spring.core.io.DefaultResourceLoader;
import com.codefish.framework.spring.core.io.Resource;

import java.util.Properties;

/**
 * properties资源文件加载类
 *
 * @author <a href='https://gitee.com/hzf2281'>codefish</a>
 * @version 1.0.0
 * @since 2022/11/04 下午 01:37
 */
public class PropertiesLoaderSupport {
    /**
     * 目标资源对象
     */
    private Resource resource;
    private String location;

   public PropertiesLoaderSupport(){

   }

    public PropertiesLoaderSupport(String location) {
        this.location = location;
    }

    public void loadProperties(Properties properties) {
        refreshResourceIfNecessary();
        PropertiesLoaderUtils.fillProperties(properties, resource);
    }

    public void setLocation(String location) {
        this.location = location;
        this.resource = null;
    }

    protected void refreshResourceIfNecessary() {
        if (this.resource == null) {
            DefaultResourceLoader loader = new DefaultResourceLoader();
            this.resource = loader.getResource(location);
        }
    }

}
